//
//  WHTabBarController.swift
//  8.31-WH微博
//
//  Created by 吴涵 on 15/8/31.
//  Copyright © 2015年 wuhan. All rights reserved.
//

import UIKit

class WHTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewControllers()
       print("viewDidLoad")
       
    }
    /// 在视图即将创建的时候，这个时候所有的控件都已经创建好 最后创建占位Btn 然后他就显示在了最上面 可以被点击到
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        print("viewWillAppear")
         setBtnFrame()
    }
/// 设置占位Btn的frame同时在addBtn.frame时 调用了addBtn的懒加载方法
    private func setBtnFrame() {
    
        let num = self.childViewControllers.count
        
        let w:CGFloat = tabBar.bounds.width / CGFloat(num)
        
        let rect = CGRect(x: 0, y: 0, width: w, height: tabBar.bounds.height)
        
        addBtn.frame = CGRectOffset(rect, 2*w, 0)
        
    }
/// 增加子控制器，根据
    private func addChildViewControllers() {
      // 本来是给tabBar设置渲染颜色 被抽取到appdelegate里面
//        tabBar.tintColor = UIColor.orangeColor()
        addChildViewController(WHHomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(WHMessageViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(WHDiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(WHProfileViewController(), title: "我", imageName: "tabbar_profile")
        
    }

    /// 增加控制器的方法
    ///
    /// :param: vc        控制器的类型
    /// :param: title     title 一个标题 相当于2个= vc.navigationItem.title +vc.tabBarItem.title
    /// :param: imageName 下面tabBarItem的图片的设置
    private func addChildViewController(vc: UIViewController,title: String,imageName: String ) {
        
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imageName)
        
        let nav:UINavigationController = UINavigationController(rootViewController: vc)
        
       addChildViewController(nav)
    }
    /// 为中间增加的控制器 下面的tabbaritem设置button按钮 占据创建时候的位置
    private lazy var addBtn: UIButton = {
    
        let btn:UIButton = UIButton(type: UIButtonType.Custom)
        /// 设置btn的属性
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
/// 注意使用self.tabBar.addSubview(btn)
        self.tabBar.addSubview(btn)
        
        btn.addTarget(self, action: "btnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
        
    }()
    
   @objc private func btnClick(){
    
        print("qweqweqweqwe")
    }

}
